/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.sortable;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQSortableUtils
{

	/**
	 * Create a JQSortableBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQSortableBuilder
	 */
	public static JQSortableBuilder create(String id, String key)
	{
		return new JQSortableBuilder(new JQSortable(id, (key == null ? JQSortablePlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQSortableBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQSortableBuilder
	 */
	public static JQSortableBuilder create(String id)
	{
		return new JQSortableBuilder(new JQSortable(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQSortableBuilder
	{
		/** JQuery component */
		private JQSortable component;
		/** JQuery default options */
		private JQSortableOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQSortableBuilder(JQSortable component)
		{
			this.component = component;
			this.options = new JQSortableOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the items option value
		 * 
		 * @param items
		 *            sortable items
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder items(String items)
		{
			options.setItems(items);
			return this;
		}

		/**
		 * Sets the connectWith option value
		 * 
		 * @param connectWith
		 *            sortable connectWith
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder connectWith(String connectWith)
		{
			options.setConnectWith(connectWith);
			return this;
		}

		/**
		 * Sets the handle option value
		 * 
		 * @param handle
		 *            sortable handle
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder handle(String handle)
		{
			options.setHandle(handle);
			return this;
		}

		/**
		 * Sets the helper option value
		 * 
		 * @param helper
		 *            sortable helper
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder helper(String helper)
		{
			options.setHelper(helper);
			return this;
		}

		/**
		 * Sets the containment option value
		 * 
		 * @param containment
		 *            sortable containment
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder containment(String containment)
		{
			options.setContainment(containment);
			return this;
		}

		/**
		 * Sets the axis option value
		 * 
		 * @param axis
		 *            sortable axis
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder axis(String axis)
		{
			options.setAxis(axis);
			return this;
		}

		/**
		 * Sets the cancel option value
		 * 
		 * @param cancel
		 *            sortable cancel
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder cancel(String cancel)
		{
			options.setCancel(cancel);
			return this;
		}

		/**
		 * Sets the cursor option value
		 * 
		 * @param cursor
		 *            sortable cursor
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder cursor(String cursor)
		{
			options.setCursor(cursor);
			return this;
		}

		/**
		 * Sets the place holder option value
		 * 
		 * @param placeholder
		 *            sortable placeHolder
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder placeholder(String placeholder)
		{
			options.setPlaceholder(placeholder);
			return this;
		}

		/**
		 * Sets the tolerance option value
		 * 
		 * @param tolerance
		 *            sortable tolerance
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder tolerance(String tolerance)
		{
			options.setTolerance(tolerance);
			return this;
		}

		/**
		 * Sets the revert option value
		 * 
		 * @param revert
		 *            sortable revert
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder revert(boolean revert)
		{
			options.setRevert(revert);
			return this;
		}

		/**
		 * Sets the scroll option value
		 * 
		 * @param scroll
		 *            sortable scroll
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder scroll(boolean scroll)
		{
			options.setScroll(scroll);
			return this;
		}

		/**
		 * Sets the dropOnEmpty option value
		 * 
		 * @param dropOnEmpty
		 *            sortable dropOnEmpty
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder dropOnEmpty(boolean dropOnEmpty)
		{
			options.setDropOnEmpty(dropOnEmpty);
			return this;
		}

		/**
		 * Sets the forceplaceholderSize option value
		 * 
		 * @param forcePlaceholderSize
		 *            sortable force placeholder size
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder forcePlaceholderSize(boolean forcePlaceholderSize)
		{
			options.setForcePlaceholderSize(forcePlaceholderSize);
			return this;
		}

		/**
		 * Sets the scroll speed option value
		 * 
		 * @param scrollSpeed
		 *            sortable scroll speed
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder scrollSpeed(int scrollSpeed)
		{
			options.setScrollSpeed(scrollSpeed);
			return this;
		}

		/**
		 * Sets the scroll sensitivity option value
		 * 
		 * @param scrollSensitivity
		 *            sortable scroll sensitivity
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder scrollSensitivity(int scrollSensitivity)
		{
			options.setScrollSensitivity(scrollSensitivity);
			return this;
		}

		/**
		 * Sets the zIndex option value
		 * 
		 * @param index
		 *            sortable zIndex
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder zIndex(int index)
		{
			options.setZIndex(index);
			return this;
		}

		/**
		 * Sets the opacity option value
		 * 
		 * @param opacity
		 *            sortable opacity
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder opacity(float opacity)
		{
			options.setOpacity(opacity);
			return this;
		}

		/**
		 * Sets the delay option value
		 * 
		 * @param delay
		 *            sortable delay
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder delay(int delay)
		{
			options.setDelay(delay);
			return this;
		}

		/**
		 * Sets the distance option value
		 * 
		 * @param distance
		 *            sortable distance
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder distance(int distance)
		{
			options.setDistance(distance);
			return this;
		}

		/**
		 * Sets the start function
		 * 
		 * @param startScript
		 *            sortable start function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder startScript(String startScript)
		{
			options.setStartScript(startScript);
			return this;
		}

		/**
		 * Sets the stop function
		 * 
		 * @param stopScript
		 *            sortable stop function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder stopScript(String stopScript)
		{
			options.setStopScript(stopScript);
			return this;
		}

		/**
		 * Sets the beforeStop function
		 * 
		 * @param beforeStopScript
		 *            sortable beforeStop function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder beforeStopScript(String beforeStopScript)
		{
			options.setBeforeStopScript(beforeStopScript);
			return this;
		}

		/**
		 * Sets the update function
		 * 
		 * @param updateScript
		 *            sortable update function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder updateScript(String updateScript)
		{
			options.setUpdateScript(updateScript);
			return this;
		}

		/**
		 * Sets the sort function
		 * 
		 * @param sortScript
		 *            sortable sort function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder sortScript(String sortScript)
		{
			options.setSortScript(sortScript);
			return this;
		}

		/**
		 * Sets the change function
		 * 
		 * @param changeScript
		 *            sortable change function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder changeScript(String changeScript)
		{
			options.setChangeScript(changeScript);
			return this;
		}

		/**
		 * Sets the receive function
		 * 
		 * @param receiveScript
		 *            sortable receive function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder receiveScript(String receiveScript)
		{
			options.setReceiveScript(receiveScript);
			return this;
		}

		/**
		 * Sets the remove function
		 * 
		 * @param removeScript
		 *            sortable remove function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder removeScript(String removeScript)
		{
			options.setRemoveScript(removeScript);
			return this;
		}

		/**
		 * Sets the over function
		 * 
		 * @param overScript
		 *            sortable over function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder overScript(String overScript)
		{
			options.setOverScript(overScript);
			return this;
		}

		/**
		 * Sets the out function
		 * 
		 * @param outScript
		 *            sortable out function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder outScript(String outScript)
		{
			options.setOutScript(outScript);
			return this;
		}

		/**
		 * Sets the activate function
		 * 
		 * @param activateScript
		 *            sortable activate function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder activateScript(String activateScript)
		{
			options.setActivateScript(activateScript);
			return this;
		}

		/**
		 * Sets the deactivate function
		 * 
		 * @param deactivateScript
		 *            sortable deactivate function
		 * @return JQSortableBuilder
		 */
		public JQSortableBuilder deactivateScript(String deactivateScript)
		{
			options.setDeactivateScript(deactivateScript);
			return this;
		}

		/**
		 * @return JQSortable
		 */
		public JQSortable build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}